<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-row>
        <el-col :span="11">
          <el-form-item label="车牌号" prop="truckId">
            <el-select filterable style="width: 100%"  :disabled="settlementStatus"  v-model="dataForm.truckId" placeholder="车牌号" @change="truckIdChange">
              <el-option
                v-for="item in vehicelCodeList"
                :key="item.id"
                :label="item.licensePlate"
                :value="+item.id">
              </el-option>
            </el-select>
          </el-form-item>

        </el-col>
        <el-col :span="11">
          <el-form-item label="驾驶员" prop="driverId">
            <el-select filterable style="width: 100%" :disabled="settlementStatus"  v-model="dataForm.driverId" placeholder="驾驶员">
              <el-option
                v-for="item in driverList"
                :key="item.id"
                :label="item.name"
                :value="+item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="收货单位" prop="companyId">
            <el-select filterable style="width: 100%"  :disabled="settlementStatus"  v-model="dataForm.companyId" placeholder="收货单位">
              <el-option
                v-for="item in receiveCompanyList"
                :key="item.id"
                :label="item.name"
                :value="+item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="发出站" prop="sendStation">
            <el-input v-model="dataForm.sendStation" :disabled="settlementStatus"  placeholder="发出站"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="运输日期" prop="shipmentDate">
            <el-date-picker style="width: 100%"
                            :disabled="settlementStatus"
                            type="date"
                            v-model="dataForm.shipmentDate"
                            :editable="false"
                            value-format="yyyy-MM-dd"
                            placeholder="运输日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="运输车次" prop="shipmentCount">
            <el-input type="number" :disabled="settlementStatus"   v-model="dataForm.shipmentCount" placeholder="运输车次"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col  :span="11">
          <el-form-item label="矿发吨数" prop="sendWeight">
            <el-input type="number" :disabled="settlementStatus"  v-model="dataForm.sendWeight" placeholder="矿发吨数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="实收吨数" prop="receiveWeight">
            <el-input type="number" :disabled="settlementStatus"  v-model="dataForm.receiveWeight" placeholder="实收吨数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="11">
          <el-form-item label="每吨运费" prop="price">
            <el-input type="number" :disabled="settlementStatus"  v-model="dataForm.price" placeholder="每吨运费"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item  label="燃料费" prop="fuelAmount">
            <el-input type="number"  v-model="dataForm.fuelAmount" placeholder="燃料费(油/电)"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="11">
          <el-form-item label="工资" prop="salary">
            <el-input type="number"  v-model="dataForm.salary" placeholder="工资"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="预支消费" prop="prepayAmount">
            <el-input type="number"  v-model="dataForm.prepayAmount" placeholder="其他费用"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="11">
          <el-form-item label="其他费用" prop="otherAmount">
            <el-input type="number"  v-model="dataForm.otherAmount" placeholder="其他费用"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="11">
          <el-form-item label="备注" prop="note">
            <el-input type="textarea" v-model="dataForm.note" placeholder="备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Utils from '@/utils/utils'
  export default {
    props:['vehicelCodeList', 'receiveCompanyList', 'driverList'],
    data () {
      return {
        visible: false,
        settlementStatus:0,
        dataForm: {
          id: 0,
          truckId: '',
          driverId: '',
          companyId: '',
          sendStation: '',
          sendWeight: '',
          receiveWeight: '',
          price: 0,
          shipmentCount: 1,
          fuelAmount: 0,
          salary: 0,
          otherAmount: 0,
          shipmentDate: '',
          note: '',
          prepayAmount:0
        },
        dataRule: {
          truckId: [
            { required: true, message: '车牌号不能为空', trigger: 'blur' }
          ],
          driverId: [
            { required: true, message: '驾驶员不能为空', trigger: 'blur' }
          ],
          companyId: [
            { required: true, message: '收货单位不能为空', trigger: 'blur' }
          ],
          sendStation: [
            { required: true, message: '发出站不能为空', trigger: 'blur' }
          ],
          sendWeight: [
            { required: true, message: '矿发吨数不能为空', trigger: 'blur' }
          ],
          receiveWeight: [
            { required: true, message: '实收吨数不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '每吨运费不能为空', trigger: 'blur' }
          ],
          fuelAmount: [
            { required: true, message: '燃料费(油/电)不能为空', trigger: 'blur' }
          ],
          salary: [
            { required: true, message: '工资不能为空', trigger: 'blur' }
          ],
          otherAmount: [
            { required: true, message: '其他费用不能为空', trigger: 'blur' }
          ],
          totalAmount: [
            { required: true, message: '总支出费用不能为空', trigger: 'blur' }
          ],
          shipmentDate: [
            { required: true, message: '运输日期不能为空', trigger: 'blur' }
          ],
          shipmentCount: [
            { required: true, message: '运输车次不能为空', trigger: 'blur' }
          ],
          note: [
            { required: false, message: '', trigger: 'blur' }
          ],
          prepayAmount: [
            { required: true, message: '预支消费不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
      this.dataForm.shipmentDate =Utils.getCurrentDate();
    },
    methods: {
      truckIdChange(value){

        this.dataForm.driverId =this.vehicelCodeList.filter(item => item.id === value)[0].defaultDriverId || ''
      },
      init (id,settlementStatus, type, data) {
        if (type === 'copy') {
          this.dataForm.id = ''
          this.visible = true
          this.dataForm = data
          return
        }
        this.settlementStatus = settlementStatus
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()

          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tms/shipmentrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.shipmentRecord
                // this.dataForm.truckId = data.shipmentRecord.truckId
                // this.dataForm.driverId = data.shipmentRecord.driverId
                // this.dataForm.companyId = data.shipmentRecord.companyId
                // this.dataForm.sendStation = data.shipmentRecord.sendStation
                // this.dataForm.sendWeight = data.shipmentRecord.sendWeight
                // this.dataForm.receiveWeight = data.shipmentRecord.receiveWeight
                // this.dataForm.price = data.shipmentRecord.price
                // this.dataForm.shipmentCount = data.shipmentRecord.shipmentCount
                // this.dataForm.fuelAmount = data.shipmentRecord.fuelAmount
                // this.dataForm.salary = data.shipmentRecord.salary
                // this.dataForm.otherAmount = data.shipmentRecord.otherAmount
                // this.dataForm.totalAmount = data.shipmentRecord.totalAmount
                // this.dataForm.shipmentDate = data.shipmentRecord.shipmentDate
                // this.dataForm.note = data.shipmentRecord.note
                // this.dataForm.prepayAmount = data.shipmentRecord.prepayAmount

              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit:Utils.debounce(function (){
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tms/shipmentrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'truckId': this.dataForm.truckId,
                'driverId': this.dataForm.driverId,
                'companyId': this.dataForm.companyId,
                'sendStation': this.dataForm.sendStation,
                'sendWeight': this.dataForm.sendWeight,
                'receiveWeight': this.dataForm.receiveWeight,
                'price': Number(this.dataForm.price).toFixed(2),
                'fuelAmount': Number(this.dataForm.fuelAmount).toFixed(2),
                'salary': Number(this.dataForm.salary).toFixed(2),
                'otherAmount': Number(this.dataForm.otherAmount).toFixed(2),
                'shipmentCount': +this.dataForm.shipmentCount,
                'shipmentDate': this.dataForm.shipmentDate,
                'note': this.dataForm.note,
                'prepayAmount': Number(this.dataForm.prepayAmount).toFixed(2)
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },1000)
    }
  }
</script>
